/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aptech.business;

import com.aptech.DAO.NewsDAO;
import com.aptech.domain.JPA.BaseSearchCondition;
import com.aptech.domain.JPA.Category;
import com.aptech.domain.JPA.News;
import com.aptech.domain.JPA.NewsCategory;
import com.aptech.domain.JPA.NewsId;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author Admin
 */
public class NewsManager extends BaseManager implements INewsManager {
    private NewsDAO newsDAO;
    private static Logger logger = Logger.getLogger(NewsManager.class);

    /**
     * @return the newsDAO
     */
    public NewsDAO getNewsDAO() {
        return newsDAO;
    }

    /**
     * @param newsDAO the newsDAO to set
     */
    public void setNewsDAO(NewsDAO newsDAO) {
        this.newsDAO = newsDAO;
    }
    public List<News> getAllNews() throws Exception {
        try {
            List<News> newsList = newsDAO.findAll(null);
            return newsList;
        } catch (Exception e) {
            logger.debug(e);
        }
        return null;
    }
    public News getNewsById(int newsId) throws Exception {
        try {
            News news = newsDAO.findById(1);
            System.out.println(news);
            return news;
        } catch (Exception e) {
            logger.debug(e);
        }
        return null;
    }
    public News getNewsDetail(BaseSearchCondition baseSearchCondition) throws Exception {
        try {
            News news = newsDAO.findById(baseSearchCondition.getNews().getNewsId());
            return news;
        } catch (Exception e) {
            logger.debug(e);
        }
        return null;
    }
    public List<Category> getAllCategory() throws Exception {
        try {
            List<Category> categoryList = categoryDAO.findAll(null);
            return categoryList;
        } catch (Exception e) {
            logger.debug(e);
        }
        return null;
    }
    public List<News> getNewsForCategory(BaseSearchCondition baseSearchCondition) throws Exception {
        try {
            List<News> newsList = null;
            if(baseSearchCondition.getCategory().getCategoryId() != null) {
                NewsCategory newsCategory = newsCategoryDAO.findById(baseSearchCondition.getCategory().getCategoryId());
                newsList = new ArrayList<News>();
                newsList.addAll(newsCategory.getNewses());
            }
            return newsList;
        } catch (Exception e) {
            logger.debug(e);
        }
        return null;
    }
    

}

